//
//  FinancialViewController.swift
//  FirstSwiftProduct
//
//  Created by 知合金服-Mini on 2017/10/30.
//  Copyright © 2017年 范泽华. All rights reserved.
//

import UIKit

class FinancialViewController: BaseViewController ,UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,zhiboDelegate{
    
    func didSelectIndex(lives: livess) {
        let xjjVC = XIAOJIEJIEViewController()
        xjjVC.liveHotData = lives
//        self.navigationController?.pushViewController(xjjVC, animated: true)
        self.present(xjjVC, animated: true) {
            
        }
    }
    

    var scrollerView = UIScrollView()
    let zhiboVC = ZHIBOViewController()
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 116 * scaleHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellId = "HomeTableViewCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId) as? HomeTableViewCell
        
        cell?.selectionStyle = UITableViewCellSelectionStyle.none
        cell!.commit()
        return cell!
        
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        //        cell.responds(to: #selector())
        //        cell.separatorInset = UIEdgeInsets.zero
        //        cell.layoutMargins = UIEdgeInsets.zero
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let detailVc = DetailViewController()
        self.navigationController?.pushViewController(detailVc, animated: true)
    }
    
    
    private var LoginTableView = UITableView()
    var headBackView = UIView()
    var lineView = UIView()
    
    var refreshControl = UIRefreshControl()
    
    func refreshData() -> Void {
        refreshControl.beginRefreshing()
//
        if refreshControl.isRefreshing {
            refreshControl.endRefreshing()
        }
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.isHidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.tabBarController?.tabBar.isHidden = false
        self.title = "理财"
        self.titleLabel.text = "你猜我是不是理财"
        
        // 头部视图
        headBackView = UIView.init(frame: CGRect(x: 0, y: CGFloat(kStatus_Navigation_height), width: SCREEN_WIDTH, height: 35 * scaleWidth))
        headBackView.backgroundColor = UIColor.white
        self.view.addSubview(headBackView)
        //投资按钮
        let leftBtn = UIButton.init(type: UIButtonType.system)
        leftBtn.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH / 2, height: 35 * scaleWidth)
        leftBtn.setTitle("投资", for: UIControlState.normal)
        leftBtn.setTitleColor(UIColor.red, for: UIControlState.normal)
        leftBtn.addTarget(self, action: #selector(changeSize), for: UIControlEvents.touchUpInside)
        leftBtn.tag = 10086
        headBackView.addSubview(leftBtn)
        //转让按钮
        let rightBtn = UIButton.init(type: UIButtonType.system)
        rightBtn.frame = CGRect(x: SCREEN_WIDTH / 2, y: 0, width: SCREEN_WIDTH / 2, height: 35 * scaleWidth)
        rightBtn.setTitle("小姐姐", for: UIControlState.normal)
        rightBtn.setTitleColor(ktextColor, for: UIControlState.normal)
        rightBtn.addTarget(self, action: #selector(changeSize), for: UIControlEvents.touchUpInside)
        rightBtn.tag = 10087
        headBackView.addSubview(rightBtn)
        
        //头部红线
        lineView = UIView.init(frame: CGRect(x: 0, y: 34 * scaleWidth, width: SCREEN_WIDTH / 2, height: 1))
        lineView.backgroundColor = UIColor.red
        headBackView.addSubview(lineView)
    

        //UITableView 初始化
        LoginTableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCRENN_HEIGHT - kTabBarHight - 35 * scaleWidth - (CGFloat)(kStatus_Navigation_height)))
        //        self.view.backgroundColor = UIColor.colorWithHexString(color: "#EFEEF4")
        LoginTableView.backgroundColor = UIColor.colorWithHexString(color: "#EFEEF4")
        LoginTableView.delegate = self
        LoginTableView.dataSource = self;
        LoginTableView.register(UINib(nibName: "HomeTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "HomeTableViewCell")
        LoginTableView.separatorStyle = UITableViewCellSeparatorStyle.none
        
//        view.addSubview(LoginTableView)
        LoginTableView.tableFooterView = UIView.init()
        
        //UIScrollView 初始化
        scrollerView = UIScrollView.init(frame: CGRect(x: 0, y: 35 * scaleWidth + (CGFloat)(kStatus_Navigation_height), width: SCREEN_WIDTH, height: SCRENN_HEIGHT - kTabBarHight - 35 * scaleWidth - (CGFloat)(kStatus_Navigation_height )))
//        scrollerView.backgroundColor = UIColor.red
        scrollerView.contentSize = CGSize(width: SCREEN_WIDTH * 2, height: 0)
        scrollerView.delegate = self
        scrollerView.isPagingEnabled = true
        scrollerView.isDirectionalLockEnabled = true
        self.view.addSubview(scrollerView)
        
        scrollerView.addSubview(LoginTableView)

       
        
        
//        self.navigationController?.pushViewController(zhiboVC, animated: true)
        zhiboVC.view.frame = CGRect(x: SCREEN_WIDTH, y: 0, width: SCREEN_WIDTH, height: SCRENN_HEIGHT - kTabBarHight - 35 * scaleWidth - (CGFloat)(kStatus_Navigation_height))
        zhiboVC.delegate = self
        
        scrollerView.addSubview(zhiboVC.view)
        
        refreshControl.addTarget(self, action: #selector(refreshData), for: UIControlEvents.valueChanged)
        refreshControl.attributedTitle = NSAttributedString(string: "下拉刷")
        LoginTableView.addSubview(refreshControl)
//        refreshControl.beginRefreshing()
//        refreshData()
        
        
    }
    
    func changeSize(sender:UIButton) -> Void {
        
        for button in headBackView.subviews  {
            
            if button.isKind(of: NSClassFromString("UIButton")!){
                let btn:UIButton = button as! UIButton
                
                btn.setTitleColor(ktextColor, for: UIControlState.normal)
            }
        }
        sender.setTitleColor(UIColor.red, for: UIControlState.normal)
        
//        let contance = <#value#>
        
        UIView.animate(withDuration: 0.2) {
            self.lineView.x = SCREEN_WIDTH / 2 * (CGFloat)(sender.tag - 10086)
        }
        
        scrollerView.setContentOffset(CGPoint(x: SCREEN_WIDTH  * (CGFloat)(sender.tag - 10086), y: 0), animated: true)
    }

    //UIScrollViewDelegate
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let point = scrollerView.contentOffset
        let index = point.x / SCREEN_WIDTH
        
        for button in headBackView.subviews  {
            
            if button.isKind(of: NSClassFromString("UIButton")!){
                let btn:UIButton = button as! UIButton
                
                btn.setTitleColor(ktextColor, for: UIControlState.normal)
            }
        }
        let btn:UIButton = headBackView.viewWithTag(Int(10086 + index)) as! UIButton

        btn.setTitleColor(UIColor.red, for: UIControlState.normal)
        
        UIView.animate(withDuration:0.2) {
            self.lineView.x = SCREEN_WIDTH / 2 * index
        }
        
        scrollerView.setContentOffset(CGPoint(x: SCREEN_WIDTH  * index, y: 0), animated: true)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollerView.contentOffset.y == SCREEN_WIDTH * 2 {
            zhiboVC.getData()
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
